 var fs = require('fs');
var path=require('path');
 module.exports = function (BrowserWindow, nativeImage) {
 	var windowAllClosed = false,
 		inteval = 1000,
 		PrinterInPeration = false;
	 var list = [];
	 
	 var  packagejson = './package'+'.json';
 	var config = require(packagejson).window;
 	if (config.icon) {
 		config.icon =path.join(__dirname, config.icon)
 	}
 	var webform = new BrowserWindow(config);

	 webform.loadURL('file://' + __dirname + '/index.html');
	 
 	//webform.webContents.openDevTools();
	 
	 this.onPrinter = function (PrinterDB, Callback) {
 		list.push({
 			db: PrinterDB,
 			Callback: Callback
 		});
 		setTimeout(function(){
 			setval();
 		}, inteval);

 	}
 	var setval = function () {
 		if (list.length > 0 && PrinterInPeration == false) {
 			PrinterInPeration = true;
 			var info = list[0];
 			var PrinterDB = info.db;
 			if (PrinterDB.preview) {
 				webform.show();
 			}
 			webform.webContents.executeJavaScript('window.onPrinter(' + JSON.stringify(PrinterDB) + ')');
 			webform.setSize(PrinterDB.width + 20, PrinterDB.height + 30);
 		}
 	}
 	var start = function () {
 		setval();
 	}
 	webform.on('close', function(event) {
 		if (!windowAllClosed) {
 			webform.hide();

 			if (list.length > 0) {
 				var info = list.splice(0, 1)[0];
 				PrinterInPeration = false;
 				if (info.Callback) {
 					info.Callback(info.db)
 				}
 				start()
 			}
 		} else {
 			windowAllClosed = false;
 			webform.close(true);
 			return
 		}
 		event.preventDefault();
 	});
 	this.close = function () {
 		windowAllClosed = true;
 		webform.hide();
 		webform.close(true);
 	}

 	start();
 };